//
//  DiscoverViewController.m
//  BaseAPP
//
//  Created by Evisu on 16/12/14.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DisconverViewCell.h"

@interface DiscoverViewController ()<UITableViewDataSource, UITableViewDelegate>{

    NSArray *_titleNames;
    NSArray *_imageNames;
}

@property (nonatomic, strong)UITableView *tableView;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
    _titleNames = @[@[@"朋友圈"],@[@"扫一扫", @"摇一摇"],@[@"附近的人", @"漂流瓶"],@[@"购物", @"游戏"]];
    
    _imageNames = @[@[@"ff_IconShowAlbum"],@[@"ff_IconQRCode", @"ff_IconShake"],@[@"ff_IconLocationService", @"ff_IconBottle"],@[@"ff_IconBottle", @"ff_IconBottle"]];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.sd_layout
    .topSpaceToView(self.view, 64)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomSpaceToView(self.view, 49);
}

- (UITableView *)tableView {

    if (!_tableView) {
        
        _tableView = [[UITableView alloc] init];
        _tableView.backgroundColor = KBACKGROUND_COLOR;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width_sd, 20)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    
    return _tableView;
}

#pragma mark - tableView dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return _titleNames.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray *titleOfSection = _titleNames[section];
    
    return titleOfSection.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *reusedId = @"discover";
    
    DisconverViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId];
    
    if (!cell) {
        
        cell = [[DisconverViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];
    }
        
    NSArray *titles = _titleNames[indexPath.section];
    NSString *titleText = titles[indexPath.row];
    
    NSArray *images = _imageNames[indexPath.section];
    NSString *imageStr = images[indexPath.row];
    
    cell.labTitle.text = titleText;
    UIImage *img = [UIImage imageNamed:imageStr];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    cell.ivIcon.image = img;
    
    return cell;
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    static NSString *headerReusedId = @"header";
    
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReusedId];
    
    if (!headerView) {
        
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerReusedId];
        
    }
    
    headerView.contentView.backgroundColor = KBACKGROUND_COLOR;
    
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selected = NO;
    
    DMLog(@"did clicked");
    
    BaseViewController *VC = [[BaseViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

//取消tableView-headerView的粘性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 20;
        
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
}


@end
